package com.example.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.controller.vo.EvaluationsVo;
import com.example.entity.Evaluations;
import com.example.mapper.EvaluationsMapper;
import com.example.mapper.EvaluationsVoMapper;
import com.example.service.IEvaluationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author yzy
 * @since 2025-08-04
 */
@Service
public class EvaluationsServiceImpl extends ServiceImpl<EvaluationsMapper, Evaluations> implements IEvaluationsService {

    @Autowired
    private EvaluationsMapper evaluationsMapper;

    @Autowired
    private EvaluationsVoMapper evaluationsVoMapper;

    @Override
    public Page<Evaluations> selectList(Page<Evaluations> page, QueryWrapper<Evaluations> queryWrapper) {
        Page<Evaluations> page1 = evaluationsMapper.selectpage(page, queryWrapper);
        return page1;

    }

    @Override
    public List<EvaluationsVo> listTree(Integer productId) {
        List<EvaluationsVo> list = evaluationsVoMapper.selectlist(productId);

        // 创建ID到索引的映射，提高查找效率
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            map.put(list.get(i).getId(), i);
        }

        List<EvaluationsVo> topLevelComments = new ArrayList<>();

        for (EvaluationsVo evaluationsVo : list) {
            Integer fatherId = evaluationsVo.getFatherId();

            if (fatherId == null || fatherId == 0) {
                // 如果没有父评论ID，说明是顶级评论
                topLevelComments.add(evaluationsVo);
            } else {
                // 查找父评论
                Integer fatherIndex = map.get(fatherId);
                if (fatherIndex != null && fatherIndex < list.size()) {
                    EvaluationsVo father = list.get(fatherIndex);
                    if (father.getChildren() == null) {
                        father.setChildren(new ArrayList<>());
                    }
                    // 将当前评论添加到父评论的子评论列表中
                    father.getChildren().add(evaluationsVo);
                } else {
                    // 如果父评论不存在，将其作为顶级评论处理
                    topLevelComments.add(evaluationsVo);
                }
            }
        }

        return topLevelComments;
    }

    @Override
    public IPage<EvaluationsVo> selectEvalVoPage(Integer pageNum, Integer pageSize, String productName, String userName) {
        // 创建分页对象
        Page<EvaluationsVo> page = new Page<>(pageNum, pageSize);

        // 构建查询条件
        QueryWrapper<EvaluationsVo> queryWrapper = new QueryWrapper<>();

        // 按商品名称筛选（关联商品表）
        if (org.springframework.util.StringUtils.hasText(productName)) {
            queryWrapper.like("p.name", productName);  // p是商品表别名（对应XML中的product p）
        }

        // 按用户名筛选（关联用户表）
        if (org.springframework.util.StringUtils.hasText(userName)) {
            queryWrapper.like("u.user_name", userName);  // u是用户表别名（对应XML中的user u）
        }

        // 按评论时间倒序（最新评论在前）
        queryWrapper.orderByDesc("e.create_time");

        // 调用mapper的联表查询方法
        return baseMapper.selectEvalVoPage(page, queryWrapper);
    }

}
